#include "playscene.h"
#include <QDebug>
#include <QMenuBar>
#include <QPainter>
#include "mypushbutton.h"
#include <QLabel>
#include "coin.h"
#include "dataconfig.h"
#include <QPropertyAnimation>   //动画

//PlayScene::PlayScene(QWidget *parent)
//    : QMainWindow{parent}
//{

//}

PlayScene::PlayScene(int levelNum)
{
    QString str = QString("关卡%1").arg(levelNum);
    qDebug() << str;
    chooseLevel = levelNum;

    //初始化游戏场景
    this->setFixedSize(320, 590);
    //设置图标
    this->setWindowIcon(QPixmap(":/res/Coin0001.png"));
    //设置标题
    this->setWindowTitle(str);
    //创建菜单栏
    QMenuBar * menubar = menuBar();
    setMenuBar(menubar);
    //创建开始菜单
    QMenu * startMenu = menubar->addMenu("开始");
    //创建退出菜单项
    QAction * quitAction = startMenu->addAction("退出");
    connect(quitAction, &QAction::triggered, [=](){
        this->close();
    });


    //返回按钮
    MyPushButton * btn_back = new MyPushButton(":/res/BackButton.png", ":/res/BackButtonSelected.png");
    btn_back->setParent(this);
    btn_back->move(this->width() - btn_back->width() - 10, this->height() - btn_back->height() - 10);
    //点击返回
    connect(btn_back, &QPushButton::clicked, [=](){
        //qDebug() << "返回";
        emit clickedBack();
    });


    //显示当前关卡数（左下角）
    QLabel * label = new QLabel(this);
    QFont font;
    font.setFamily("方正粗黑宋简体");
    font.setPointSize(20);
    //格式化显示
    QString str2 = QString("Level:%1").arg(levelNum);
    label->setText(str2);
    //设置位置及大小
    label->setGeometry(30, this->height() - 50, 120, 50);
    //设置Font
    label->setFont(font);


    //金币数据
    dataConfig config;
    //将该关卡数据读出
    for(int i = 0; i < 4; i++)
    {
        for(int j = 0; j < 4; j++)
        {
            this->gameArray[i][j] = config.mData[levelNum][i][j];
        }
    }

    //胜利图片显示（初始屏幕外 胜利后向下显示）
    QLabel * winLabel = new QLabel(this);
    QPixmap winPix;
    winPix.load(":/res/LevelCompletedDialogBg.png");
    winLabel->setGeometry(0, 0, winPix.width(), winPix.height());
    winLabel->setPixmap(winPix);
    winLabel->move((this->width() - winPix.width()) * 0.5, -winPix.height());

    //显示金币背景图案
    for(int i = 0; i < 4; i++)
    {
        for(int j = 0; j < 4; j++)
        {
            //背景图片
            QLabel * label = new QLabel(this);
            label->setGeometry(0, 0, 50, 50);
            label->setPixmap(QPixmap(":/res/BoardNode.png"));
            label->move(60 + i * 50, 200 + j * 50);

            //显示金币
            QString str;
            if(gameArray[i][j] == 1)
                str = ":/res/Coin0001.png";
            else
                str = ":/res/Coin0008.png";

            //创建金币
            Coin * coin = new Coin(str);
            coin->setParent(this);
            coin->move(62 + i * 50, 203 + j * 50);

            //将金币放入金币的二维数组中
            this->coinArray[i][j] = coin;

            //给金币属性赋值
            coin->posX = i;
            coin->posY = j;
            coin->flag = this->gameArray[i][j];

            //点击金币 进行翻转
            connect(coin, &Coin::clicked, [=](){
                //将所有的金币禁止点击
                for(int i = 0; i < 4; i++)
                {
                    for(int j = 0; j < 4; j++)
                    {
                        this->coinArray[i][j]->isWin = true;
                    }
                }

                //翻转点击的金币
                coin->changeFlag();
                this->gameArray[coin->posX][coin->posY] = this->gameArray[coin->posX][coin->posY] == 0 ? 1 : 0;

                //翻转周围的金币
                //延时周围按钮的翻转
                QTimer::singleShot(250, this, [=](){
                    //右侧
                    if(coin->posX + 1 <= 3)
                    {
                        this->coinArray[coin->posX + 1][coin->posY]->changeFlag();
                        this->gameArray[coin->posX + 1][coin->posY] = this->gameArray[coin->posX + 1][coin->posY] == 0 ? 1 : 0;
                    }
                    //左侧
                    if(coin->posX - 1 >= 0)
                    {
                        this->coinArray[coin->posX - 1][coin->posY]->changeFlag();
                        this->gameArray[coin->posX - 1][coin->posY] = this->gameArray[coin->posX - 1][coin->posY] == 0 ? 1 : 0;
                    }
                    //上侧
                    if(coin->posY - 1 >= 0)
                    {
                        this->coinArray[coin->posX][coin->posY - 1]->changeFlag();
                        this->gameArray[coin->posX][coin->posY - 1] = this->gameArray[coin->posX][coin->posY - 1] == 0 ? 1 : 0;
                    }
                    //下侧
                    if(coin->posY + 1 <= 3)
                    {
                        this->coinArray[coin->posX][coin->posY + 1]->changeFlag();
                        this->gameArray[coin->posX][coin->posY + 1] = this->gameArray[coin->posX][coin->posY + 1] == 0 ? 1 : 0;
                    }

                    //判断是否胜利
                    this->isWin = true; //先为真
                    for(int i = 0; i < 4; i++)
                    {
                        for(int j = 0; j < 4; j++)
                        {
                            if(this->gameArray[i][j] == 0)
                            {
                                this->isWin = false;
                                i = 5;
                                break;
                            }
                        }
                    }
                    if(this->isWin)
                    {
                        qDebug() << "胜利";
                        //将所有金币的 isWin 置true
                        for(int i = 0; i < 4; i++)
                        {
                            for(int j = 0; j < 4; j++)
                            {
                                this->coinArray[i][j]->isWin = true;
                            }
                        }
                        //显示胜利的图片
                        //置为最顶层显示
                        winLabel->raise();
                        QPropertyAnimation * animation = new QPropertyAnimation(winLabel, "geometry");
                        //设置间隔时间
                        animation->setDuration(1000);
                        //设置开始位置
                        animation->setStartValue(QRect(winLabel->x(), winLabel->y(), winLabel->width(), winLabel->height()));
                        //设置结束位置
                        animation->setEndValue(QRect(winLabel->x(), winLabel->y() + 300, winLabel->width(), winLabel->height()));
                        //设置缓和曲线
                        animation->setEasingCurve(QEasingCurve::OutBounce);
                        //开始
                        animation->start();
                    }
                    else    //未胜利
                    {
                        //再次开放所有按钮 允许点击
                        for(int i = 0; i < 4; i++)
                        {
                            for(int j = 0; j < 4; j++)
                            {
                                this->coinArray[i][j]->isWin = false;
                            }
                        }
                    }
                });
            });
        }
    }
}
//重写绘画事件
void PlayScene::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    QPixmap pix;
    pix.load(":/res/PlayLevelSceneBg.png");
    painter.drawPixmap(0, 0, this->width(), this->height(), pix);

    //加载标题
    pix.load(":/res/Title.png");
    painter.drawPixmap(this->width() * 0.5 - pix.width() * 0.5, 30, pix.width(), pix.height(), pix);
}
